

package com.maoshi.shop.api.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.bean.enums.OrderStatus;
import com.maoshi.shop.bean.model.Order;
import com.maoshi.shop.bean.model.OrderItem;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.bean.app.dto.ProdCommDataDto;
import com.maoshi.shop.bean.app.dto.ProdCommDto;
import com.maoshi.shop.bean.app.param.ProdCommParam;
import com.maoshi.shop.bean.model.ProdComm;
import com.maoshi.shop.security.api.util.SecurityUtils;
import com.maoshi.shop.service.OrderItemService;
import com.maoshi.shop.service.OrderService;
import com.maoshi.shop.service.ProdCommService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

@RestController
@RequestMapping("/p/prodComm")
@Api(tags = "评论接口")
@AllArgsConstructor
public class ProdCommController {

    private final ProdCommService prodCommService;

    private final OrderItemService orderItemService;

    private final OrderService orderService;


    @GetMapping("/prodCommPageByUser")
    @ApiOperation(value = "根据用户返回评论分页数据", notes = "传入页码")
    public ResponseEntity<IPage<ProdCommDto>> getProdCommPage(PageParam page) {
        return ResponseEntity.ok(prodCommService.getProdCommDtoPageByUserId(page, SecurityUtils.getUser().getUserId()));
    }



    @PostMapping
    @ApiOperation(value = "添加评论")
    public ResponseEntity<Void> saveProdCommPage(@Valid @RequestBody ProdCommParam prodCommParam) {
        Long orderItemId = prodCommParam.getOrderItemId();

        OrderItem orderItem = orderItemService.getById(orderItemId);
        if (orderItem == null) {
            throw new MaoshiShopBindException("订单项不存在");
        }
        if (Objects.equals(orderItem.getCommSts(), 1)) {
            throw new MaoshiShopBindException("改订单项已评论，请勿重复评论");
        }

        String userId = SecurityUtils.getUser().getUserId();

        Order order = orderService.getOrderByOrderNumberAndUserId(orderItem.getOrderNumber(),userId, true);

        if (!Objects.equals(order.getStatus(), OrderStatus.SUCCESS.value())) {
            throw new MaoshiShopBindException("请确认收货后再进行评论");
        }

        prodCommService.comm(orderItem, prodCommParam);

        return ResponseEntity.ok().build();
    }

    @GetMapping("/getProdComment")
    @ApiOperation(value = "根据itemId获取评论", notes = "根据itemId获取评论")
    public ResponseEntity<ProdComm> getProdComment(Long orderItemId) {

        ProdComm prodComm = prodCommService.getOne(new LambdaUpdateWrapper<ProdComm>().eq(ProdComm::getOrderItemId, orderItemId).eq(ProdComm::getUserId,SecurityUtils.getUser().getUserId()));
        return ResponseEntity.ok(prodComm);
    }
}
